package action.concreteActions;

import grammer.DirectObject;
import grammer.ModalObject;
import grammer.Predicate;
import grammer.components.ObjectEnum;
import grammer.components.PowerEnum;
import grammer.components.PredicateEnum;
import houseHold.House;
import person.PersonAchive;
import timer.Timer;
import action.ExecuteActionInterface;

/**
 * 
 * @author Minodor
 * 
 *         Used for predicates:
 *         turns_on,turns_off,drinks,sets,turns_everything_off
 */
public class SimpleUseAction extends ExecuteActionInterface {
	boolean active = false;
	int modifier;
	ObjectEnum oType = null;
	int type = 0;

	private boolean windowStatus;

	public SimpleUseAction(Timer executeTime, Predicate p) {
		super(executeTime);
		if (p.getPredicate().equals(PredicateEnum.TURNS_EVERYTHING_OFF)) {
			active = false;
			type = 1;
		}
		if (p.getPredicate().equals(PredicateEnum.OPENS_WINDOW)) {
			windowStatus = true;
			type = 0;
		}
		if (p.getPredicate().equals(PredicateEnum.CLOSES_WINDOW)) {
			windowStatus = false;
			type = 0;
		}
	}

	public SimpleUseAction(Timer executeTime, Predicate p, DirectObject d) {
		super(executeTime);
		if (p.getPredicate().equals(PredicateEnum.TURNS_ON))
			active = true;
		if (p.getPredicate().equals(PredicateEnum.TURNS_OFF))
			active = false;
		oType = d.getObject();
		type = 2;
	}

	public SimpleUseAction(Timer executeTime, Predicate p, DirectObject d,
			ModalObject m) {
		super(executeTime);
		oType = d.getObject();
		switch ((PowerEnum) (m.getMember())) {
		case LOW:
			modifier = 70;
			break;
		case MEDIUM:
			modifier = 100;
			break;
		case HIGH:
			modifier = 130;
			break;
		}
		type = 3;

	}

	@Override
	public int execute() {
		switch (type) {
		case 1: {
			if (PersonAchive.getPerson(this.getOnwer()) != null) {
				String locationName = PersonAchive.getPerson(this.getOnwer())
						.getCurrentLocation().getCurrent();
				House.getInstance().getRoom(locationName).turnEverythingOff();
			}
			break;
		}
		case 0: {
			if (PersonAchive.getPerson(this.getOnwer()) != null) {
				String locationName = PersonAchive.getPerson(this.getOnwer())
						.getCurrentLocation().getCurrent();
				House.getInstance().getRoom(locationName).setWindowStatus(
						windowStatus);
			}
			break;
		}
		case 2: {
			String locationName = PersonAchive.getPerson(this.getOnwer())
					.getCurrentLocation().getCurrent();
			House.getInstance().getRoom(locationName).getItemByType(oType)
					.setActive(active);
			System.out.println(House.getInstance().getRoom(locationName).getItemByType(oType));
			break;
		}
		case 3: {
			String locationName = PersonAchive.getPerson(this.getOnwer())
					.getCurrentLocation().getCurrent();
			House.getInstance().getRoom(locationName).getItemByType(oType)
					.setConsumptionCoefficient(modifier);
			break;
		}
		}
		return 0;
	}

	@Override
	public int getEndTimer() {
		return executeTime.getTime();
	}

	@Override
	public int getExecuteTimer() {
		return executeTime.getTime();
	}

	@Override
	public int getStartTimer() {
		return executeTime.getTime();
	}

}
